//
//  ThirdViewController.m
//  WisdomZhuHai
//
//  Created by yewei on 13-8-21.
//  Copyright (c) 2013年 wyd. All rights reserved.
//

#import "ThirdViewController.h"
#import "Common.h"
//#import "PlaceHomeViewController.h"
#import "PersonViewController.h"
#import "LeadToLoginInViewController.h"
#import "LoginViewController.h"
#import "TransforController.h"
#import "EmptyViewController.h"
#import "SettingViewController.h"

#define UM_KEY @"54574621fd98c55970005aec"
#define KEEP_PASSAWORD_LIST @"keepPassword.plst"
#define KEY_ACCOUNT @"key_account"
#define KEY_PASSWORD @"key_passord"
#define KEY_IFKEEPPASSWORD @"key_ifkeeppassord"
#define USER_INFO_LIST @"userinfo.text"
#define LOGINSTATUS @"loginStatus.plst"

@interface ThirdViewController ()<aftefLoginProtocol>

@property (nonatomic,assign) int selectIdx;

@property (nonatomic,retain) PersonViewController* personView;
@property (nonatomic, retain) LeadToLoginInViewController *memberLeadView;
@property (nonatomic, strong) UIBarButtonItem *btn_setting;

@end

@implementation ThirdViewController

@synthesize selectIdx;



- (id)init
{
    self = [super init];
    if(self)
    {
        self.tabBarItem.image = [UIImage imageNamed:@"Information/我的2.png"];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"Information/我的1.png"];
        self.tabBarItem.title = @"我的";
    }
    return self;
}

-(void)dealloc
{
    [_personView release];
    [super dealloc];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.placeHomeViewCtrl = [[[PlaceHomeViewController alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain] autorelease];
//    self.placeHomeViewCtrl.navigationCtrl = self.navigationController;
//    [self.view addSubview:self.placeHomeViewCtrl.view];
//    NSString *loginName = [ProtocolManager shareInstance].loginName;
//    if(loginName != nil){
//        [self changeStatusAfterLogin:1];
//    }else{
//        [self changeStatusAfterLogin:0];
//    }
//    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
//    self.navigationController.navigationBar.translucent = YES;
////    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"sidebar/nav2@2x.png"] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self changeStatusAfterLogin:0];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)changeStatusAfterLogin:(int)option{
    switch (option) {
        case 0:{
            NSLog(@"%s || current status is not sign in yet.", __FUNCTION__);
            if(self.memberLeadView == nil){
                self.memberLeadView = [[LeadToLoginInViewController alloc]initWithFrame:self.view.bounds];
            }
            self.memberLeadView.navigationCtrl = self.navigationController;
            if(self.personView != nil){
                [self.personView.view removeFromSuperview];
                //                [self.personView release];
            }
//            if(self.btn_setting == nil){
//                
//            }
            
            self.memberLeadView.delegate = self;
            [self.view addSubview:self.memberLeadView.view];
        }
            break;
        case 1:{
            NSLog(@"%s || current status is alreadyt signed in.", __FUNCTION__);
            self.personView  = [TransforController sharedInstanceOfPersonViewController];
            [self.personView setFrame:CGRectMake(0, -45, 320, 613)];
            self.personView.navigationCtrl = self.navigationController;
            
            if(self.memberLeadView != nil){
                [self.memberLeadView.view removeFromSuperview];
                //                [self.memberLeadView release];
            }
//            if(self.btn_setting != nil){
//                self.navigationItem.rightBarButtonItem = nil;
//                [self.btn_setting release];
//            }
            [self.personView getUserInfo];
            [self.view addSubview:self.personView.view];
        }
        default:
            break;
    }
    self.btn_setting = [[UIBarButtonItem alloc]initWithTitle:@"更多" style:UIBarButtonItemStyleDone target:self action:@selector(gotoSettingViewController)];
    self.navigationItem.rightBarButtonItem = self.btn_setting;
}

- (void)doCheckIfLogined{
    NSString *filePath = [FileManager getFullDocumentPath:LOGINSTATUS];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
    NSLog(@"%s || dic:%@",__FUNCTION__, dic);
    if(dic != nil){
        if([[Common valueForKey:@"LoginStatus" dic:dic] isEqualToString:@"0001"]){
            
            NSString *loginName = [dic objectForKey:@"loginName"];
            NSString *password = [dic objectForKey:KEY_PASSWORD];
            
            [LoginViewController loginWithTarget:self selector:@selector(getLoginCallBack:) account:loginName passWord:password];
        }else if([[Common valueForKey:@"LoginStatus" dic:dic]isEqualToString:@"0002"]){
            [self loginWithTarget:self selector:@selector(getLoginCallBack2:) mobile:[Common valueForKey:@"Mobile" dic:dic]];
        }
    }
}

- (void)getLoginCallBack:(NSDictionary *)dic{
    [[Common shareInstance] stopLoadingView];
    if(dic == nil)
    {
        NSLog(@"%s || dic is nil", __FUNCTION__);
        return;
    }else{
        if([dic objectForKey:@"Sex"] == nil || [dic objectForKey:@"Sex"] == NULL || [dic objectForKey:@"Sex"] == [NSNull null]){
            [dic setValue:@"" forKey:@"Sex"];
        }
        if([dic objectForKey:@"Birthday"] == nil || [dic objectForKey:@"Birthday"] == NULL || [dic objectForKey:@"Birthday"] == [NSNull null]){
            [dic setValue:@"1980-01-01" forKey:@"Birthday"];
        }
        if([dic objectForKey:@"Industry_to"] == nil || [dic objectForKey:@"Industry_to"] == NULL || [dic objectForKey:@"Industry_to"] == [NSNull null]){
            [dic setValue:@"" forKey:@"Industry_to"];
        }
        if([dic objectForKey:@"Email"] == nil || [dic objectForKey:@"Email"] == NULL || [dic objectForKey:@"Email"] == [NSNull null]){
            [dic setValue:@"" forKey:@"Email"];
        }
        if([dic objectForKey:@"Address"] == nil || [dic objectForKey:@"Address"] == NULL || [dic objectForKey:@"Address"] == [NSNull null]){
            [dic setValue:@"" forKey:@"Address"];
        }
        if([[dic objectForKey:@"Birthday"] isEqualToString:@"0000-00-00"]){
            [dic setValue:@"1980-01-01" forKey:@"Birthday"];
        }
    }
    NSLog(@"%s || dic:%@",__FUNCTION__, dic);
    NSUserDefaults *accountInfo = [NSUserDefaults standardUserDefaults];
    //[accountInfo setObject:[dic objectForKey:P_USERID] forKey:P_USERID];
    [accountInfo setObject: [dic objectForKey:@"Mobile"] forKey:@"Mobile"];
    [accountInfo setObject: [dic objectForKey:@"Username"] forKey:@"Username"];
    [accountInfo setObject:[dic objectForKey:@"Sex"] forKey:@"Sex"];
    [accountInfo setObject:[dic objectForKey:@"Birthday"] forKey:@"Birthday"];
    [accountInfo setObject:[dic objectForKey:@"Industry_to"] forKey:@"Industry_to"];
    [accountInfo setObject:[dic objectForKey:@"Email"] forKey:@"Email"];
    [accountInfo setObject:[dic objectForKey:@"Address"] forKey:@"Address"];
    [accountInfo synchronize];
    
    NSString *respCode = [Common valueForKey:P_RESPCODE dic:dic];
    if([respCode isEqualToString:CODE_HANDLE_SUCCESS])
    {
        [ProtocolManager shareInstance].sid = [Common valueForKey:P_SID dic:dic];
        [ProtocolManager shareInstance].userId = [[dic objectForKey:@"userId"] integerValue];
        [ProtocolManager shareInstance].loginName = [dic objectForKey:@"Loginname"];
        [ProtocolManager shareInstance].mobile = [dic objectForKey:@"Mobile"];
        [self changeStatusAfterLogin:1];
        NSMutableDictionary *savingDic = [NSMutableDictionary dictionary];
        NSString *readFilePath = [FileManager getFullDocumentPath:LOGINSTATUS];
        NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:readFilePath];
        //[savingDic setValue:[dic objectForKey:P_SID] forKey:P_SID];
        [savingDic setValue:[dic objectForKey:P_USERID] forKey:P_USERID];
        [savingDic setValue:[readDic objectForKey:@"loginName"] forKey:@"loginName"];
        [savingDic setValue:[readDic objectForKey:KEY_PASSWORD] forKey:KEY_PASSWORD];
        [savingDic setValue:[dic objectForKey:@"Username"] forKey:@"Username"];
        [savingDic setValue:[dic objectForKey:P_MOBILENUMBER] forKey:P_MOBILENUMBER];
        [savingDic setValue:[dic objectForKey:@"Sex"] forKey:@"Sex"];
        [savingDic setValue:[dic objectForKey:@"Birthday"] forKey:@"Birthday"];
        [savingDic setValue:[dic objectForKey:@"Email"] forKey:@"Email"];
        [savingDic setValue:[dic objectForKey:@"Address"] forKey:@"Address"];
        [savingDic setValue:[dic objectForKey:@"Industry_to"] forKey:@"Industry_to"];
        [savingDic setValue:@"0001" forKey:@"LoginStatus"];
        [savingDic writeToFile:[FileManager getFullDocumentPath:LOGINSTATUS]atomically:YES];
//        NSString *filePath = [FileManager getFullDocumentPath:LOGINSTATUS];
//        NSDictionary *tempDic = [NSDictionary dictionaryWithContentsOfFile:filePath];
        
        //[dic writeToFile:[FileManager getFullDocumentPath:LOGINSTATUS]atomically:YES];
    }
}

-(void)loginWithTarget:(id)target selector:(SEL)sel mobile:(NSString *)mobile{
    NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
    //app版本
    NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];
    //手机型号
    NSString* phoneModel = [[UIDevice currentDevice] model];
    //手机系统版本
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    //物理地址
    NSString* macAddr = [Common getMacAddr];
    [[ProtocolManager shareInstance]mobileLogin:mobile mobileModel:phoneModel loginCity:@"" appVersion:versionNum OSversion:phoneVersion mac:macAddr target:target selector:sel];
}

- (void)getLoginCallBack2:(NSDictionary *)dic{
    //    [[Common shareInstance] stopLoadingView];
    if(dic == nil)
    {
        NSLog(@"%s || dic is nil", __FUNCTION__);
        return;
    }else{
        if([dic objectForKey:@"Sex"] == nil || [dic objectForKey:@"Sex"] == NULL || [dic objectForKey:@"Sex"] == [NSNull null]){
            [dic setValue:@"" forKey:@"Sex"];
        }
        if([dic objectForKey:@"Birthday"] == nil || [dic objectForKey:@"Birthday"] == NULL || [dic objectForKey:@"Birthday"] == [NSNull null]){
            [dic setValue:@"1980-01-01" forKey:@"Birthday"];
        }
        if([dic objectForKey:@"Industry_to"] == nil || [dic objectForKey:@"Industry_to"] == NULL || [dic objectForKey:@"Industry_to"] == [NSNull null]){
            [dic setValue:@"" forKey:@"Industry_to"];
        }
        if([dic objectForKey:@"Email"] == nil || [dic objectForKey:@"Email"] == NULL || [dic objectForKey:@"Email"] == [NSNull null]){
            [dic setValue:@"" forKey:@"Email"];
        }
        if([dic objectForKey:@"Address"] == nil || [dic objectForKey:@"Address"] == NULL || [dic objectForKey:@"Address"] == [NSNull null]){
            [dic setValue:@"" forKey:@"Address"];
        }
        if([[dic objectForKey:@"Birthday"] isEqualToString:@"0000-00-00"]){
            [dic setValue:@"1980-01-01" forKey:@"Birthday"];
        }
    }
    NSLog(@"%s || dic:%@",__FUNCTION__, dic);
    NSDictionary *dic_temp = [Common valueForKey:@"info" dic:dic];
    NSUserDefaults *accountInfo = [NSUserDefaults standardUserDefaults];
    //[accountInfo setObject:[dic objectForKey:P_USERID] forKey:P_USERID];
    [accountInfo setObject: [dic_temp objectForKey:@"Mobile"] forKey:@"Mobile"];
    [accountInfo setObject: [dic_temp objectForKey:@"Username"] forKey:@"Username"];
    [accountInfo setObject:[dic_temp objectForKey:@"Sex"] forKey:@"Sex"];
    [accountInfo setObject:[dic_temp objectForKey:@"Birthday"] forKey:@"Birthday"];
//    [accountInfo setObject:[dic_temp objectForKey:@"Industry_to"] forKey:@"Industry_to"];
    [accountInfo setObject:[dic_temp objectForKey:@"Email"] forKey:@"Email"];
    [accountInfo setObject:[dic_temp objectForKey:@"Address"] forKey:@"Address"];
    [accountInfo synchronize];
    
    NSString *respCode = [Common valueForKey:P_RESPCODE dic:dic];
    if([respCode isEqualToString:CODE_HANDLE_SUCCESS])
    {
        [ProtocolManager shareInstance].sid = [Common valueForKey:@"sessionId" dic:dic];
        [ProtocolManager shareInstance].userId = [[dic_temp objectForKey:@"ID"] integerValue];
        [ProtocolManager shareInstance].loginName = [dic objectForKey:@"loginname"];
        [ProtocolManager shareInstance].mobile = [dic_temp objectForKey:@"Mobile"];
        [ProtocolManager shareInstance].isLoginedByPhone = YES;
        [self changeStatusAfterLogin:1];
        NSMutableDictionary *savingDic = [NSMutableDictionary dictionary];
        NSString *readFilePath = [FileManager getFullDocumentPath:LOGINSTATUS];
        NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:readFilePath];
        //[savingDic setValue:[dic objectForKey:P_SID] forKey:P_SID];
        [savingDic setValue:[dic_temp objectForKey:P_USERID] forKey:P_USERID];
        [savingDic setValue:[readDic objectForKey:@"loginName"] forKey:@"loginName"];
        [savingDic setValue:[readDic objectForKey:KEY_PASSWORD] forKey:KEY_PASSWORD];
        [savingDic setValue:[dic_temp objectForKey:@"Username"] forKey:@"Username"];
        [savingDic setValue:[dic_temp objectForKey:P_MOBILENUMBER] forKey:P_MOBILENUMBER];
        [savingDic setValue:[dic_temp objectForKey:@"Sex"] forKey:@"Sex"];
        [savingDic setValue:[dic_temp objectForKey:@"Birthday"] forKey:@"Birthday"];
        [savingDic setValue:[dic_temp objectForKey:@"Email"] forKey:@"Email"];
        [savingDic setValue:[dic_temp objectForKey:@"Address"] forKey:@"Address"];
//        [savingDic setValue:[dic_temp objectForKey:@"Industry_to"] forKey:@"Industry_to"];
        [savingDic setValue:@"0002" forKey:@"LoginStatus"];
        [savingDic writeToFile:[FileManager getFullDocumentPath:LOGINSTATUS]atomically:YES];
        //        NSString *filePath = [FileManager getFullDocumentPath:LOGINSTATUS];
        //        NSDictionary *tempDic = [NSDictionary dictionaryWithContentsOfFile:filePath];
        
        //[dic writeToFile:[FileManager getFullDocumentPath:LOGINSTATUS]atomically:YES];
    }
}


- (void)gotoSettingViewController{
    EmptyViewController *emptyViewController = [[EmptyViewController alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
    SettingViewController *setting = [[SettingViewController alloc]initWithStyle:UITableViewStyleGrouped];
    [setting.view setFrame:CGRectMake(0, 0, 320, 523)];
    //    setting.delegate = self;
    //    setting.hidesBottomBarWhenPushed = YES;
    emptyViewController.hidesBottomBarWhenPushed = YES;
    [emptyViewController.view addSubview:setting.view];
    [emptyViewController.view addSubview:[TransforController shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:-65 andTitle:@"设置"]];
    setting.navigationCtrl = self.navigationController;
    [self.navigationController pushViewController:emptyViewController animated:YES];
}


@end
